package com.ljx.recruitment.interview.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ljx.recruitment.common.enums.CommonStatusTypeEnum;
import com.ljx.recruitment.interview.constant.JobConstant;
import com.ljx.recruitment.interview.domain.entity.JobProcess;
import com.ljx.recruitment.interview.domain.vo.req.JobProcessSubmitReq;
import com.ljx.recruitment.interview.enums.JobBaseProcessEnum;
import com.ljx.recruitment.interview.mapper.JobProcessMapper;
import com.ljx.recruitment.interview.service.JobProcessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: LiJunXi
 * @Description:
 * @Date: 2024-12-26  11:29
 */
@Service
public class JobProcessServiceImpl extends ServiceImpl<JobProcessMapper, JobProcess> implements JobProcessService {

    @Autowired
    private JobProcessMapper jobProcessMapper;


    @Override
    public void saveByReq(JobProcessSubmitReq req, int newCount) {
        JobBaseProcessEnum processEnum = JobBaseProcessEnum.cache.get(req.getJobProcessKey());
        JobProcess jobProcess = new JobProcess();
        jobProcess.setJobId(req.getJobId());
        jobProcess.setBatch(newCount);
        jobProcess.setProcessName(processEnum.getValue() + newCount);
        jobProcess.setProcessKey(req.getJobProcessKey());
        jobProcessMapper.insert(jobProcess);
    }

    @Override
    public void deleteByReq(JobProcessSubmitReq req, int nowCount) {
        JobProcess jobProcess = jobProcessMapper.selectOne(new LambdaQueryWrapper<JobProcess>()
                .eq(JobProcess::getJobId, req.getJobId())
                .eq(JobProcess::getProcessKey, req.getJobProcessKey())
                .eq(JobProcess::getBatch, nowCount)
                .eq(JobProcess::getStatus, CommonStatusTypeEnum.NORMAL.getKey()));
        jobProcess.setStatus(CommonStatusTypeEnum.BAN.getKey());
        jobProcessMapper.updateById(jobProcess);
    }

    @Override
    public List<JobProcess> getProcessByJobId(Long jobId) {
        return jobProcessMapper.selectList(new LambdaQueryWrapper<JobProcess>()
                .eq(JobProcess::getJobId, jobId)
                .eq(JobProcess::getStatus, CommonStatusTypeEnum.NORMAL.getKey())
                .orderByAsc(JobProcess::getProcessKey)
                .orderByAsc(JobProcess::getBatch));
    }

    @Override
    public Map<Long, Map<Integer, List<JobProcess>>> getProcessMapByJobIds(List<Long> jobIds) {
        return lambdaQuery()
                .in(JobProcess::getJobId, jobIds).eq(JobProcess::getStatus, CommonStatusTypeEnum.NORMAL.getKey())
                .list()
                .stream()
                .collect(Collectors.groupingBy(JobProcess::getJobId,
                        Collectors.groupingBy(JobProcess::getProcessKey)));

    }
}
